package cs470.agent;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import Code.BZRClient;
import Code.Tank;


public class ReallyDumbAgent extends AbstractAgent{

	private long lastShot;
	private long lastAction;
	private State nextState;
	
	private Random random = new Random();
	
	public ReallyDumbAgent(final BZRClient bzrClient) {
		super(bzrClient);
		nextState = State.MOVING;
	}
	
	public void run() {
		setRunning(true);
		
		while(isRunning()) {
			
			long currentTime = System.currentTimeMillis();
			if(currentTime >= lastShot) {
				//shoot();
				shotFired();
			}
			
			if(currentTime >= lastAction) {
				switch(nextState) {
					case MOVING:
						turn(0);
						drive(1);
						starting();
						nextState = State.TURNING;
						break;
					case TURNING:
						turn(45);
						stop();
						stopping();
						nextState = State.MOVING;
						break;
					default:
						throw new IllegalStateException("next State must be defined");
				}
			}
		}
	}

    private void starting() {
        lastAction = System.currentTimeMillis() + 3000l; //+ random.nextLong() % 5000;
    }

    private void stopping() {
        lastAction = System.currentTimeMillis() + 1000l;
    }

    private void shotFired() {
        lastShot = System.currentTimeMillis() + 1500l + random.nextLong() % 1000;
    }
	
	public enum State {
		TURNING, MOVING
	}
}
